#ifndef NR_IO_XML_H
#define NR_IO_XML_H

#include "nr_util.h"
#include "nr_io.h"
#include <QtXml/QDomDocument>

#define NR_DEVICE_TYPE_FILE  "\\DeviceSchema.xml"
#define NR_DEVICE_ITEMS_FILE "\\DeviceItems.xml"

class nr_io_xml : public nr_io
{
public:
    nr_io_xml(void);
    
    ~nr_io_xml(void);

    virtual nr_error_t connect() { return NR_ERROR_NONE; }

    virtual nr_error_t write_data(nr_data* data) { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t write_event(nr_event* evt) { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t query_data(const nr_device_id_t& device_id, QDateTime &begin_time, 
        QDateTime &end_time, QList<nr_data*>& data_list) { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t query_event(const nr_device_id_t& device_id, QDateTime &begin_time, 
        QDateTime &end_time, QList<nr_event*>& data_list) { return NR_ERROR_NOT_IMPLEMENTED; }

    virtual nr_error_t read_device_types(nr_device_types& device_types);
    virtual nr_error_t write_device_types(nr_device_types& device_types);
    virtual nr_error_t read_device_items(nr_device_item_server& item_server);
    virtual nr_error_t write_device_items(nr_device_item_server& item_server);

    virtual nr_error_t read_roles() { return NR_ERROR_NOT_IMPLEMENTED; }

    void set_DeviceTypeFile(const QString& xml_file) { m_DeviceTypeFile=xml_file; }
    void set_DeviceItemsFile(const QString& xml_file) { m_DeviceItemsFile=xml_file; }

protected:
    nr_device_type* get_device_type(const nr_id_t &type_id);

    //read_device_types from xml
    bool item_node_parse(QDomElement &item_node, nr_device_type_property &item_device_type_property);
    bool items_node_to_property(QDomElement &items_node, nr_device_type_properties &item_device_type_propertys);
    bool node_to_device_type(QDomElement &type_node, nr_device_type &device_type);
    
    //write_device_types to xml
    bool create_attributte_to_element(QDomDocument &doc, QDomElement &node, const QString &domattrname, QString &domattrvalue);
    bool create_item_node(QDomDocument &doc, QDomElement &item_node, nr_device_type_property &item_property);
    bool create_nodes_to_items(QDomDocument &doc, QDomElement &nodes, nr_device_type_properties &item_device_type_propertys);
    bool create_node_to_devicetypes(QDomDocument &doc, QDomElement &devicetype_node, nr_device_type &device_type);
   
    //read_device_items from xml
    bool parse_device_item_server_parameters(QDomElement &node, nr_device_item_server &device_item_server);
    bool parse_device_group_parameters(QDomElement &node, nr_device_group &device_group);
    bool parse_device_item_paramaters(QDomElement &node, nr_device_item &device_item);
    bool parse_property_parameters(QDomElement &node, nr_device_property &device_property);
    bool analyze_property_node(QDomElement &property_node, nr_device_property &device_property);
    bool analyze_device_item_node(QDomElement &device_item_node, nr_device_item &device_item);
    bool analyze_device_group_node(QDomElement &device_group_node, nr_device_group &device_group, nr_device_item &parent_device_item);
   
    //write_device_items to xml
    bool create_node_property(QDomDocument &doc, QDomElement &node, nr_device_property &device_property);
    bool create_node_device_item(QDomDocument &doc, QDomElement &node, nr_device_item &device_item);
    bool create_node_device_group(QDomDocument &doc, QDomElement &node, nr_device_group &device_group);
    bool create_node_device_item_server(QDomDocument &doc,nr_device_item_server &device_item_server);

private:
    nr_device_types *m_device_types;
    QString m_DeviceTypeFile;
    QString m_DeviceItemsFile;
};

#endif